package gui.dialog;

import gui.GUIController;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import game.player.Computer;
import game.player.Human;
/*
 * This class is used when the player wants to set up a new game.
 * The SetUpGameDialog will show a JDialog and asks the client to
 * choose a player mode and enter player names.
 */

public class SetUpGameDialog extends JDialog {

	private final int BLACK = 1, RED = 2;
	private JTextField jtxfName1;
	private JTextField jtxfName2;
	private JRadioButton jrbtHvsH;
	private JRadioButton jrbtHvsC;
	private JRadioButton jrbtCvsC;
	private GUIController controller;
	private JPanel p1 = new JPanel();
	private JButton btnOK = new JButton("OK");

	public SetUpGameDialog(GUIController controller) {

		this.controller = controller;
		setTitle("Enter name and select player type.");
		setSize(355, 150);

		setModal(true);
		p1.setLayout(new GridLayout(1, 2, 1, 1));
		add(p1, BorderLayout.CENTER);
		add(setOKButton(), BorderLayout.SOUTH);
		setLocationRelativeTo(null);
		
		radioBtnPanel();
		playerNamePanel();
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setVisible(true);
	}
	/*
	 * Creates a new game with Humans versus Human playing type.
	 */

	private void playHumanVsHuman() {
		controller.getGameManager().addPlayers(
				  new Human(getName1(), 1, BLACK, controller.getGameManager()),
				  new Human(getName2(), 2, RED, controller.getGameManager()));
		initializeGamePlay();
	}
	/*
	 * Creates a new game with Humans versus Computer playing type.
	 */

	private void playHumanVsComputer() {
		controller.getGameManager().addPlayers(
				  new Human(getName1(), 1, BLACK, controller.getGameManager()),
				  new Computer(getName2(), 2, RED, controller.getGameManager().getGameGrid()));
		initializeGamePlay();
	}
	/*
	 * Creates a new game with Computer versus Computer playing type.
	 */

	private void playComputerVsComputer() {
		controller.getGameManager().addPlayers(
				  new Computer(getName1(), 1, BLACK, controller.getGameManager().getGameGrid()),
				  new Computer(getName2(), 2, RED, controller.getGameManager().getGameGrid()));
		initializeGamePlay();
	}

	/*
	 * This method initializes the game board.
	 * Greates a game board, clears a game board and starts the game.
	 * Also set the SetUpGameDialog frame to none visability.
	 */

	private void initializeGamePlay() {
		controller.resetGameBoard();
		controller.startGame();
		setVisible(false);
	}
	/*
	 * Adds a action listener to the JButton.
	 */

	public JButton setOKButton() {
		btnOK.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if (jrbtCvsC.isSelected()) {
					playComputerVsComputer();
				} else if (jrbtHvsC.isSelected()) {
					playHumanVsComputer();
				} else if (jrbtHvsH.isSelected()) {
					playHumanVsHuman();
				}
			}
		});
		return btnOK;
	}
	/*
	 * The player name panel.
	 * Displays 2 text fields.
	 */

	private void playerNamePanel() {
		//Creates new text fields.
		jtxfName1 = new JTextField("Player 1");
		jtxfName2 = new JTextField("Player 2");
		//Creates a new panel.
		JPanel panel = new JPanel();
		//Adds border, layout and text fields to the panel.
		panel.setBorder(BorderFactory.createTitledBorder("Player Names"));
		panel.setLayout(new GridLayout(2, 1, 0, 0));
		panel.add(jtxfName1);
		panel.add(jtxfName2);
		panel.setVisible(true);
		//Add panel to the p1 panel.
		p1.add(panel);
	}
	/*
	 * This method creates the radio button panel.
	 * It contains the different player types that the client
	 * can choose from.
	 */

	private void radioBtnPanel() {
		//Creates new panel.
		JPanel radioPanel = new JPanel();
		radioPanel.setLayout(new GridLayout(5, 1));
		//Creates new button group.
		ButtonGroup bgroup = new ButtonGroup();
		//Create radio buttons.
		jrbtHvsH = new JRadioButton("Human VS Human", true);
		jrbtHvsC = new JRadioButton("Human VS Computer", false);
		jrbtCvsC = new JRadioButton("Computer VS Computer", false);
		//Add radio buttons to button group.
		bgroup.add(jrbtHvsH);
		bgroup.add(jrbtHvsC);
		bgroup.add(jrbtCvsC);
		radioPanel.setBorder(BorderFactory.createTitledBorder("Player Type"));
		radioPanel.add(jrbtHvsH);
		radioPanel.add(jrbtHvsC);
		radioPanel.add(jrbtCvsC);
		radioPanel.setVisible(true);
		p1.add(radioPanel);
	}

	private String getName1() {
		return jtxfName1.getText();
	}

	private String getName2() {
		return jtxfName2.getText();
	}
}
